package tang.com.test.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import tang.com.test.R;
import tang.com.test.listener.OnAdapterListener;

public class MainActivity extends AppCompatActivity implements OnAdapterListener {
    private RecyclerView recyclerView;
    private MyAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        initData();
    }
    private void initData() {
        List<String>  datas = new ArrayList<>();
        datas.add("当贝跳转测试页");
        mAdapter.setDatas(datas);
    }
    private void init() {
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(mAdapter = new MyAdapter());
        mAdapter.setOnAdapterListener(this);
    }
    @Override
    public void onCallBack(int pos) {
        switch (pos){
            case 0:
//                Toast.makeText(MainActivity.this,"点
// 击了item 0",Toast.LENGTH_LONG).show();
                startActivity(new Intent(MainActivity.this,DBActivity.class));
                break;
        }
    }




    class MyAdapter extends RecyclerView.Adapter {
        List<String> datas;

        OnAdapterListener l;
        public void setOnAdapterListener(OnAdapterListener l) {
            this.l = l;
        }
        public void setDatas(List<String> datas) {
            if (datas == null || datas.size() <= 0) {
                return;
            }
            this.datas = datas;
            notifyDataSetChanged();
        }
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.view_recycler_item,parent,false));
        }
        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            if (holder instanceof MyViewHolder) {
                bindMyViewHolder((MyViewHolder) holder, position);
            }
        }
        private void bindMyViewHolder(MyViewHolder holder, final int position) {
            holder.mTextView.setText(datas.get(position));
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    l.onCallBack(position);
                }
            });
        }
        @Override
        public int getItemCount() {
            return datas.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder {
            TextView mTextView;

            public MyViewHolder(View itemView) {
                super(itemView);
                mTextView= (TextView) itemView.findViewById(R.id.textview_content);
            }
        }
    }
}
